<?php

class IndexController extends Controller
{
    public function IndexAction() 
    {        
        $this->view->show();
    }
    public function LogoutAction() 
    {
        Authenticate::logout();
        Application::redirectPage("Index","Login");
        exit;
    }
    
    public function LoginAction() 
    {        
        if ($this->getRequest()->isPOST())
        {
            $result = Authenticate::auth('database', $this->getRequest()->POST('username'), 
                    $this->getRequest()->POST('password'));
            if ($result == Authenticate::AUTH_SUCCESS)
            {
                Application::redirectPage("Index","Index");
            }
            else
            {
                $params = array();
                if ($result == Authenticate::AUTH_USER_NOT_FOUND)
                {
                    $params['message-error'] = 'Usuário não encontrado.';
                }
                else 
                {
                    $params['message-error'] = 'Senha inválida.';
                }
                $this->view->setParams($params);
            }
        }
        
        $this->view->setLayout('login.html');
        $this->view->show();
    }
}

